#include<stdio.h>
#include<pthread.h>
#include<unistd.h>


int ticket=100;
pthread_mutex_t mutex;

void* route(void* args)
{
    char* val =(char*)args;
   
    while(1)
    {
           pthread_mutex_lock(&mutex);
        if(ticket > 0)
        {
            sleep(1);
            printf("%s sells ticket: %d\n",val ,ticket);
            ticket--;
            pthread_mutex_unlock(&mutex);
        }
        else{
              pthread_mutex_unlock(&mutex);
            break;
           
        }
          
    }
    

}



int main()
{
    pthread_t t1,t2,t3,t4;
    pthread_mutex_init(&mutex,NULL);

    pthread_create(&t1,NULL,route,"thead 1");
    pthread_create(&t2,NULL,route,"thead 2");
    pthread_create(&t3,NULL,route,"thead 3");
    pthread_create(&t4,NULL,route,"thead 4");

    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    pthread_join(t3,NULL);
    pthread_join(t4,NULL);

    pthread_mutex_destroy(&mutex);
}



